<!-- 散货 -->
<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :option="option"
      :page.sync="page"
      :permission="getpermission"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button
          icon="el-icon-user"
          type="text"
          v-if="getpermission('assignBtn', row)"
          size="small"
          @click="assigncarrier(row)"
          >分配承运商</el-button
        >
        <el-button
          icon="el-icon-document"
          type="text"
          size="small"
          @click="inquiryDetail(row)"
          >详情</el-button
        >
      </template>
    </avue-crud>
    <assigncarrierDialog ref="assigncarrierDialog"></assigncarrierDialog>
  </basic-container>
</template>

<script>
import assigncarrierDialog from "../components/assigncarrierDialog.vue";
import { getList } from "@/api/inquiry/domestictrade/index";
import { mapGetters } from "vuex";
import { bindCarrier } from "@/api/inquiry/carrierDialog";
export default {
  components: {
    assigncarrierDialog,
  },
  data() {
    return {
      query: {},
      form: {},
      data: [],
      workid: { workflowId: "2" },
      loading: false,
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    option() {
      return {
        height: "auto",
        calcHeight: 30,
        align: "center",
        headerAlign: "center",
        border: true,
        searchLabelWidth: 120,
        searchMenuSpan: 6,
        tip: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        searchShow: false,
        labelWidth: 120,
        column: [
          {
            label: "标题",
            prop: "title",
          },
          {
            label: "发货企业",
            prop: "shippingEnterprise",
            search: true,
          },
          {
            label: "发货地址",
            prop: "shippingAddress",
            search: true,
          },
          {
            label: "到货企业",
            prop: "arrivalEnterprise",
            search: true,
          },
          {
            label: "到货地址",
            prop: "arrivalAddress",
            search: true,
          },
          {
            label: "危化品/普货",
            prop: "goodsType",
            search: true,
          },
          {
            label: "工业/电子",
            prop: "industry",
            search: true,
          },
          {
            label: "净重",
            prop: "netWeight",
          },
          {
            label: "运输方式",
            prop: "shippingType",
          },
          {
            label: "运费类型",
            prop: "freightType",
          },
          {
            label: "产品名称",
            prop: "cpmc",
          },
          {
            label: "询价状态",
            prop: "quoteStatus",
            type: "select",
            props: {
              label: "value",
              value: "key",
            },
            search: true,
            dicUrl: `/api/blade-tms/common/SelectEnum?name=inquiryStatus`,
            display: false,
            hide: this.getpermission("showdatastatus"),
          },
        ],
      };
    },
  },
  methods: {
    getpermission(key, row) {
      switch (key) {
        case "assignBtn":
          return (
            this.permission.bulkcargo_assign_carrier &&
            row.whetherToAllocate == "0"
          );
        case "showdatastatus":
          return !this.permission.bulkcargo_domestictrade_detail_m;
        default:
          break;
      }
      return true;
    },

    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, this.workid)
      ).then((res) => {
        this.loading = false;
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
        this.$refs.crud.doLayout();
      });
    },

    inquiryDetail(row) {
      this.$router.push({
        path: "/bulkcargo/itemDetails",
        query: { id: row.id },
      });
    },

    assigncarrier(row) {
      this.$refs.assigncarrierDialog.open(row, {
        callback: (res) => {
          bindCarrier({
            inquiryId: row.id,
            ...res.configData,
            carrierId: res.selectionList,
          })
            .then(() => {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              this.$refs.assigncarrierDialog.close();
            })
            .catch((err) => {
              console.log(err);
            })
            .finally(() => {
              this.onLoad(this.page);
              this.loading = false;
            });
        },
      });
    },

    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },

    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },

    refreshChange() {
      this.onLoad(this.page, this.query);
    },
  },
};
</script>

<style></style>
